package leetcode.method.pointer;

/**
 * 乘法太多浪费计算时间
 */
public class Program122BuyAndSellTicket {

    public Program122BuyAndSellTicket() {
    }

    public int maxProfit(int[] prices) {
        if (prices.length < 2) {
            return 0;
        }
        int diff = 0, front = 1, after = 0;
        while (front < prices.length) {
            if (prices[front] < prices[front - 1]) {
                diff +=  prices[front - 1] - prices[after];
                after = front;
            }
            front++;
        }
        if (after != front - 1) {
            diff +=  prices[front - 1] - prices[after];
        }
        return diff;
    }

    public static void main(String[] args) {
        Program122BuyAndSellTicket myStack = new Program122BuyAndSellTicket();
        int[] prices = {7,1,5,3,6,4};
        System.out.println(myStack.maxProfit(prices));
        int[] prices1 = {1,2,3,4,5};
        System.out.println(myStack.maxProfit(prices1));
        int[] prices2 = {7,6,4,3,1};
        System.out.println(myStack.maxProfit(prices2));
    }
}
